﻿// <copyright file="CategoryTreeDataEditor.cs" company="Auros Ltd.">
//     Copyright (c) Auros Ltd. All rights reserved.
// </copyright>

namespace Auros.Categorisation.Umbraco
{
    using System;
    using System.Linq;
    using System.Text;
    using Auros.Categorisation.WebControls;
    using umbraco.cms.businesslogic.datatype;
    using umbraco.interfaces;

    /// <summary>
    /// Class represents the category selection data type.
    /// </summary>
    public class CategoryTreeDataEditor : umbraco.cms.businesslogic.datatype.AbstractDataEditor
    {
        private IDataPrevalue preValueEditor;

        /// <summary>
        /// Initializes a new instance of the <see cref="CategoryTreeDataEditor"/> class.
        /// </summary>
        public CategoryTreeDataEditor()
        {
            // Initialise tree control.
            this.CategoryTree = new CategoryTree();
            this.CategoryTree.Init += new EventHandler(this.CategoryTree_Init);

            // Set tree control as render control for this data type.
            this.RenderControl = this.CategoryTree;

            // Set custom event handler for saving data.
            this.DataEditorControl.OnSave += new umbraco.cms.businesslogic.datatype.AbstractDataEditorControl.SaveEventHandler(this.DataEditorControl_OnSave);
        }

        /// <summary>
        /// Gets the data editor ID.
        /// </summary>
        /// <value>The unique ID for this data editor.</value>
        public override Guid Id
        {
            get
            {
                return new Guid("0F460ACD-0678-4859-A32A-39766B8A7A5D");
            }
        }

        /// <summary>
        /// Gets the name of the data type.
        /// </summary>
        /// <value>The name of the data type.</value>
        public override string DataTypeName
        {
            get
            {
                return "Category Tree";
            }
        }

        /// <summary>
        /// Gets or sets the category tree control.
        /// </summary>
        /// <value>The category tree control.</value>
        protected CategoryTree CategoryTree
        {
            get;
            set;
        }

        /// <summary>
        /// Handles the OnSave event for the data editor.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void DataEditorControl_OnSave(EventArgs e)
        {
            int[] selectedIds = this.CategoryTree.SelectedCategories;

            string csvValue = string.Join(",", selectedIds);

            this.Data.Value = csvValue;
        }

        /// <summary>
        /// Handles the Init event of the CategoryTree control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CategoryTree_Init(object sender, EventArgs e)
        {
            this.CategoryTree.RootID = ((CategoryTreePreValueEditor)this.PrevalueEditor).RootCategoryID;

            if (this.Data != null && this.Data.Value != null)
            {
                string[] csvValue = this.Data.Value.ToString().Split(',');

                int[] idArray = csvValue.Select(i =>
                {
                    int id;

                    if (int.TryParse(i, out id))
                        return id;

                    return -1;
                })
                .ToArray();

                this.CategoryTree.SelectedCategories = idArray;
            }
        }

        /// <summary>
        /// Gets the prevalue editor.
        /// </summary>
        /// <value>The prevalue editor.</value>
        public override IDataPrevalue PrevalueEditor
        {
            get
            {
                if (this.preValueEditor == null)
                {
                    this.preValueEditor = new CategoryTreePreValueEditor(this);
                }

                return this.preValueEditor;
            }
        }
    }
}